define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Seckilldetail, $stateParams, $window, $timeout, $ionicSlideBoxDelegate, $ionicPopup, $ionicModal, $interval, GoPage, Auth, GoDialog, ChatInfo) {
		$scope.type = $stateParams.type;
		$scope.isLoad = false; //可以加载
		$scope.scope = new Object();
		var count = 1;
		$scope.scope.count = 1;
		$scope.act_id = $stateParams.act_id;
		$scope.act_type = $stateParams.type;
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
			$scope.init_data();
		}, false);
		$scope.$on('$ionicView.beforeLeave',function(){
			if($scope.timer){
				$interval.cancel($scope.timer);
			}
		})
		//加载数据
		$scope.init_data = function() {
		Seckilldetail.detail_list($scope.act_id, function(data, now_time) {
			if(data.success == true) {
				$scope.datas = data.result;
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
				$scope.init_time(data.result, now_time);
				$scope.set_timer(now_time);
				$scope.msg = '';
				//获取中奖者名单
				if(data.result.lottery_status == true && data.result.act_type == 'ended') {
					Seckilldetail.get_winninglist($scope.act_id, function(data) {
						$scope.winningData = data;
					});
				}
			} else {
				$scope.msg = data.msg;
			}

		})
		$scope.get_loginfo();
	}

	//轮播小圆点切换
	$scope.advsgo = function(index) {
			$ionicSlideBoxDelegate.$getByHandle('advs-handle').slide(index);
		}
		//滑动时候触发
	$scope.selectTab = 0;
	$scope.activeTab = function(index) {
		$scope.selectTab = index;
		$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
	};
	$scope.slideChanged = function(index) {
		//手指滑动
		$scope.selectTab = index;
	};

	//下拉刷新 判断哪个模块进行刷新
	//		$scope.doRefresh = function() {
	//			$timeout(function() {
	//				$scope.$broadcast('scroll.refreshComplete');
	//			}, 1000);
	//		};
	//		//加载更多
	//		$scope.doRefreshMore = function() {
	//			$timeout(function() {
	//				$scope.$broadcast('scroll.infiniteScrollComplete');
	//			}, 1000);
	//		};

	$scope.isHide = true; //默认隐藏
	//隐藏弹窗
	$scope.hide_apply = function() {
			$scope.isHide = true;
		}
		//显示弹窗
	$scope.show_apply = function(value) {
		$scope.isHide = false;
		$scope.word_id = value;
	}
	$scope.formate_time = function(time) {
		if(time > 0) {
			var day = parseInt(time / (3600 * 24));
			var hour = parseInt((time % (3600 * 24)) / 3600);
			if(hour < 10)
				hour = "0" + hour;
			var min = parseInt((time % 3600) / 60);
			if(min < 10)
				min = "0" + min;
			var second = time % 60;
			if(second < 10)
				second = "0" + second;
			return day + '天' + hour + '小时' + min + '分' + second + '秒';
		} else {
			return '0天0小时0分0秒';
		}
	};
	$scope.init_time = function(data, now_time) {
		console.log(data.now_time)
		if(data.now_time != undefined)
			data.now_time = data.now_time - 1;
		else {
			if($scope.act_type == 1) {
				data.start_time = data.start_time - now_time;
				$scope.act_time_name = '距开始';
			} else {
				data.now_time = data.end_time - now_time;
				$scope.act_time_name = '距结束';
			}
		}

		data.formate_time = $scope.formate_time(data.now_time);
	}
	$scope.set_timer = function(now_time) {
		$scope.timer =  $interval(function() {
			if($scope.datas.now_time != undefined)
				$scope.datas.now_time = $scope.datas.now_time - 1;
			else {
				if($scope.act_type == 1) {
					$scope.datas.now_time = $scope.datas.start_time - now_time;
				} else {
					$scope.datas.now_time = $scope.datas.end_time - now_time;
				}
			}

			$scope.datas.formate_time = $scope.formate_time($scope.datas.now_time);
		}, 1000)
	}
	$scope.go_exchangechat = function(group_id, is_member) {
			if(is_member != true) {
				Seckilldetail.join_group(group_id, function(data) {
					if(data.rtnCode == 0 || data.rtnCode == 5) {
						GoPage.go_states("exchange_interaction_chat", {
							'group_id': group_id
						});
					} else {
						GoDialog.toast(data.rtnMsg);
					}
				})
			} else {
				GoPage.go_states("exchange_interaction_chat", {
					'group_id': group_id
				});
			}
		}
		//小小图登录信息
	$scope.get_loginfo = function() {
		//判断本地是否保存有小小图登录信息
		var is_chatLogin = ChatInfo.get_isInfos();
		if(is_chatLogin == false && Auth.check_loginInfo())
			Seckilldetail.get_loginfo(function(data) {
				$scope.chat_url = data.chat_url;
				$scope.login_code = data.login_code;
				$scope.soket_url = data.soket_url;
				//初次保存小小图登录信息
				ChatInfo.save_chat_loginInfo($scope.chat_url, $scope.login_code, $scope.soket_url);
			})
	};
	$scope.go_goods = function(goods_id, product_id) {
		//			console.log(A)
		if(Auth.check_user_type() <= 4)
			GoPage.go_states("seckill_goods", {
				'goods_id': goods_id,
				'act_id': $scope.act_id,
				'product_id': product_id
			});
		else {
			GoDialog.toast('您不满足参与条件！');
		}
	}
}

controller.$inject = ['$scope', 'Seckilldetail', '$stateParams', '$window', '$timeout', '$ionicSlideBoxDelegate', '$ionicPopup', '$ionicModal', '$interval', 'GoPage', 'Auth', 'GoDialog', 'ChatInfo'];

return app.registerController("seckilldetailctrl", controller);
});